package im.starchat.chat.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.Arrays;

import butterknife.ButterKnife;
import butterknife.InjectView;
import im.starchat.chat.R;
import im.starchat.chat.XLApplication;
import im.starchat.chat.adapter.PreviewAdapter;
import im.starchat.chat.rest.RestClient;
import im.starchat.chat.rest.model.PreviewBean;
import im.starchat.chat.rest.model.api.PreviewResponse;
import im.starchat.chat.utils.Util;
import im.starchat.chat.view.swipeRefreshLayout.SwipeRefreshLayout;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by liuchao on 2/3/16.
 * 预告 fragment
 */
public class PreviewFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener{

    private static final String TAG = "ParadeFragment";


    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.swipe_refresh_layout_list)
    SwipeRefreshLayout mSwipeRefreshLayout;

    private PreviewAdapter mPreviewAdapter;
    private LinearLayoutManager mLayoutManager;
    private int mCurrentPage = RestClient.DEFAULT_PAGE;
    private boolean mHasMoreData = true;

    public PreviewFragment() {
    }

    public static PreviewFragment newInstance() {
        PreviewFragment fragment = new PreviewFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_preview, container, false);
        ButterKnife.inject(this, view);

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setOnLoadListener(this);
        mSwipeRefreshLayout.setTopColor(R.color.refresh_color_01, R.color.refresh_color_02,
                R.color.refresh_color_02, R.color.refresh_color_01);
        mSwipeRefreshLayout.setBottomColor(R.color.color_transparent, R.color.color_transparent,
                R.color.color_transparent, R.color.color_transparent);
        mSwipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeRefreshLayout.setLoadNoFull(true);


        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mPreviewAdapter = new PreviewAdapter(getActivity(), new OnPreViewCallBack() {
            @Override
            public void onAppointments(String preview_id, int position) {
                appointments(preview_id, position);
            }

            @Override
            public void onDelAppointments(String preview_id, int position) {
                delAppointments(preview_id, position);
            }
        });
        mRecyclerView.setAdapter(mPreviewAdapter);

        getPreviewList();

        return view;
    }

    private void getPreviewList(){
        XLApplication.getInstance().getRestClient().getPreviews(mCurrentPage, RestClient.DEFAULT_PREPAGE, new Callback<PreviewBean[]>() {
            @Override
            public void success(PreviewBean[] previewsBeans, Response response) {
                if (mCurrentPage == RestClient.DEFAULT_PAGE) {
                    mPreviewAdapter.refreshData(Arrays.asList(previewsBeans));
                } else {
                    mPreviewAdapter.addAll(Arrays.asList(previewsBeans));
                }
                if (previewsBeans.length < 20) {
                    mHasMoreData = false;
                }else{
                    mHasMoreData = true;
                }
                mSwipeRefreshLayout.setRefreshing(false);
                mSwipeRefreshLayout.setLoading(false);
            }

            @Override
            public void failure(RetrofitError error) {
                mSwipeRefreshLayout.setRefreshing(false);
                mSwipeRefreshLayout.setLoading(false);
            }
        });
    }




    private void appointments(String preview_id,final int position){
        XLApplication.getInstance().getRestClient().appointments(preview_id, new Callback<PreviewResponse>() {
            @Override
            public void success(PreviewResponse msgBean, Response response) {
                mPreviewAdapter.changeBookState(position, true);
                Util.addEventToCalendar(getActivity(), msgBean.getPreview().getDescription(), msgBean.getPreview().getDescription(), Util.formatStringToDate(msgBean.getPreview().getStart_at()));
            }

            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(getActivity(), "预约失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void delAppointments(String preview_id,final int position){
        XLApplication.getInstance().getRestClient().delAppointments(preview_id, new Callback<PreviewResponse>() {
            @Override
            public void success(PreviewResponse msgBean, Response response) {
                mPreviewAdapter.changeBookState(position, false);
                Toast.makeText(getActivity(), "取消预约成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(getActivity(), "取消预约失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public static interface OnPreViewCallBack{
        public void onAppointments(String preview_id, int position);
        public void onDelAppointments(String preview_id, int position);
    }

    @Override
    public void onRefresh() {
        mCurrentPage = RestClient.DEFAULT_PAGE;
        getPreviewList();
    }

    @Override
    public void onLoad() {
        if(mHasMoreData){
            mCurrentPage ++;
            getPreviewList();
        }else{
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
            Toast.makeText(getActivity(), "没有更多数据", Toast.LENGTH_SHORT).show();
        }

    }

}
